package com.xyht.medicine_management_system.modules.medicine.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xyht.medicine_management_system.common.util.TimeUtil;
import com.xyht.medicine_management_system.modules.medicine.entity.MmsMedicine;
import com.xyht.medicine_management_system.modules.medicine.entity.MmsMedicineFootprint;
import com.xyht.medicine_management_system.modules.medicine.entity.MmsMedicineSpecial;
import com.xyht.medicine_management_system.modules.medicine.mapper.MmsMedicineFootprintMapper;
import com.xyht.medicine_management_system.modules.medicine.mapper.MmsMedicineMapper;
import com.xyht.medicine_management_system.modules.medicine.mapper.MmsMedicineSpecialMapper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import static com.xyht.medicine_management_system.common.util.ParamCheckUtil.isNullOrEmpty;


/**
 * 药品
 */
@Component
public class MedicineUtil {

    @Resource
    private MmsMedicineFootprintMapper medicineFootprintMapper;
    @Resource
    private MmsMedicineSpecialMapper medicineSpecialMapper;
    @Resource
    private MmsMedicineMapper medicineMapper;

    /**
     * 添加足迹(一个用户一天只对一个药品添加足迹)
     * @param medicine 药品基本信息
     * @param userId 用户
     */
    public void addFootprint(MmsMedicine medicine,String userId){
        //当天0点
        Date startDate = TimeUtil.GetStartDate(new Date());
        //当天24点
        Date endDate = TimeUtil.GetEndDate(new Date());

        //足迹存入药品基本信息
        JSONObject medicineJson = new JSONObject();
        medicineJson.put("id",medicine.getId());
        medicineJson.put("commonNames",medicine.getCommonNames());
        medicineJson.put("salesPrice",medicine.getSalesPrice());
        medicineJson.put("specificationModel",medicine.getSpecificationModel());
        medicineJson.put("mainPic",medicine.getMainPic());
        medicineJson.put("functionalIndications",medicine.getFunctionalIndications());
        String medicineInfo = JSONObject.parseObject(JSON.toJSONString(medicineJson), String.class);


        List<MmsMedicineFootprint> footprintList = medicineFootprintMapper.selectList(new LambdaQueryWrapper<MmsMedicineFootprint>()
                .eq(MmsMedicineFootprint::getMedicineInfo, medicineInfo)
                .eq(MmsMedicineFootprint::getUserId, userId)
                .ge(MmsMedicineFootprint::getCreateTime, startDate)
                .le(MmsMedicineFootprint::getCreateTime, endDate));

        if (footprintList.isEmpty()) {
            MmsMedicineFootprint medicineFootprint = new MmsMedicineFootprint();
            medicineFootprint.setMedicineInfo(medicineInfo);
            medicineFootprint.setUserId(userId);
            medicineFootprint.setFootprintDate(new Date());
            medicineFootprintMapper.insert(medicineFootprint);
        }
    }

    /**
     * 获取商品真实价格
     * @param medicineId 药品id
     * @return  商品真实价格
     */
    public BigDecimal getRealPrice(String medicineId){

        BigDecimal realPrice;
        //校验是否有药品信息
        MmsMedicine medicine = medicineMapper.selectById(medicineId);
        if (isNullOrEmpty(medicine)) {
            return null;
        }

        //判断是否特惠商品
        List<MmsMedicineSpecial> specialList = medicineSpecialMapper.selectList(new LambdaQueryWrapper<MmsMedicineSpecial>()
                .eq(MmsMedicineSpecial::getMedicineId, medicineId));

        //该药品不是特惠商品 真正的价格为营销价格
        if (specialList == null || specialList.size() < 1) {
            realPrice = medicine.getSalesPrice();
            return realPrice;
        }
        //是特惠商品
        realPrice = specialList.get(0).getSpecialPrice();
        return realPrice;
    }
}
